#include<iostream>
const int N = 1e5;
int step[N];
using namespace std;
int main()
{
	bool flag = 1;int n, m;cin >> n >> m;int count = 0;
	while (n)
	{
		if (n % 2 == 0)
		{
			step[++count] = 2;
			n -= 2;
		}
		else
		{
			step[++count] = 1;
			n -= 1;
		}
	}
	if (count % m == 0) cout << count << endl;
	else
	{
		for (int i = count; i >= 1; i--)
		{
			if (step[i] % 2 == 0)
			{
				step[i] = 1;
				step[i + 1] = 1;
				count++;
			}
			if (count % m == 0)
			{
				flag = 0;
				break;
			}
		}
		if (flag == 0) cout << count << endl;
		else cout << "-1" << endl;
	}
	return 0;
}